<?php
namespace api\common\model;
use think\Db;
class Shop extends \think\Model 
{
    protected $autoWriteTimestamp = true;
    protected $table = 'shop';
    

    /**
     * 店铺列表和店铺的预约人数
     */
    public function selShopList($regionId,$page,$lng,$lat){
        $res;
        $numbers = 20;
        // 当前页数
        $this_page = 1;
        if($page){
            $this_page = $page;
        }
        if($lng && count($lng)>0 && $lat && count($lat)>0){
            $res = Db::query('SELECT
            t1.shopId,
            t1.shopName,
            t1.address,
            t1.status as shop_status,
            t1.type as shop_type,
            convert((st_distance(point (lng, lat), point('.$lng.', '.$lat.')) / 0.0111),decimal(10,2)) AS distance 
            FROM
            mj_shop AS t1  HAVING distance < 50  ORDER BY distance   limit '.($this_page-1)*$numbers.','.$numbers);
        }else{
            $res = Db::query('SELECT
            t1.shopId,
            t1.shopName,
            t1.address,
            t1.status as shop_status,
            t1.type as shop_type
            FROM
            mj_shop AS t1  limit '.($this_page-1)*$numbers.','.$numbers);
        }
            return $res;
    }

    /**
     * 根据id查看信息
     * @param $shopId
     * @return $data
     */
    public function selById($shopId){
        $sel = Db::name($this->table)->find($shopId);
        return $sel;
    }


    /**
     * 门店列表,员工上班用
     */
    public function shopListWork($page){
        $res;
        $numbers = 100;
        // 当前页数
        $this_page = 1;
        if($page){
            $this_page = $page;
        }
        $res = Db::query('SELECT
        shopId,
        shopName,
        status as shop_status
        FROM mj_shop limit '.($this_page-1)*$numbers.','.$numbers);
        return $res;
    }

    /**
     * 根据员工id,查看店的信息
     */
    public function selByStaff($staffId){
        $field = array('shopName','shopId','status','type');
        $sel = Db::name($this->table)->field($field)->where('staffId',$staffId)->find();
        return $sel;
    }
}
